//
//  WeatherAPIService.m
//  iWeather
//
//  Created by Vo Thi My Hanh (FSU1.BU19) on 9/17/13.
//  Copyright (c) 2013 Vo Thi My Hanh (FSU1.BU19). All rights reserved.
//

#import "APIService.h"
#import "WeatherDefines.h"
#import "JSON.h"
#import "AppDelegate.h"



@implementation APIService
@synthesize delegate = _delegate;


-(void)requestDataWithTypeOfService:(int) type
{
    NSString* url = nil;
    
    
    SEL finishSelector = @selector(trafficStuffAPIRequestFinished:) ;
    
    switch (type)
    {
        case kTrafficStuffAPI:
        {
            url = @"https://dl.dropboxusercontent.com/u/199796172/document1.json";
            finishSelector = @selector(trafficStuffAPIRequestFinished:);
            break;
        }
            
        default:
        {
            finishSelector = nil;
            break;
        }
    }
    
    
    _asiHttpRequest=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
    [_asiHttpRequest setRequestMethod:@"GET"];
    [_asiHttpRequest setDelegate:self];
    [_asiHttpRequest setDidFinishSelector:finishSelector];
    [_asiHttpRequest setTimeOutSeconds:120];
    [_asiHttpRequest startAsynchronous];
  
}
- (void) trafficStuffAPIRequestFinished:(ASIHTTPRequest *)request
{
    SBJsonParser * parser =[[SBJsonParser alloc]init];
    
    NSLog(@"%@",request.responseString);
    // parse json
    NSDictionary* data =(NSDictionary*)[parser objectWithString:request.responseString];
    if(data)
    {
        [_delegate trafficStuffAPIRequestFinished:[data objectForKey:@"locations"]];
    }
    [parser release];
    
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    [_delegate serviceRequestTimeOut];
}

- (void) dealloc
{
    if(_asiHttpRequest)
    {
        [_asiHttpRequest release];
    }
    [super dealloc];
}



@end

